package com.haiyou.dc.canal.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.dc.canal.entity.User;
import com.haiyou.dc.canal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

/**
 * @author root
 * @date 2020-04-06 20:08:01
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户表")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "保存数据")
    @PutMapping("/save")
    public R save(@RequestBody User user) {
        userService.insertSelective(user);
        return R.ok();
    }

    @ApiOperation(value = "查看数据")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        User user = userService.selectByPrimaryKey(id);
        return R.ok(user);
    }

    @ApiOperation(value = "修改数据")
    @PostMapping("/update")
    public R update(@RequestBody User user) {
        userService.updateByPrimaryKeySelective(user);
        return R.ok();
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        userService.deleteByPrimaryKey(id);
        return R.ok();
    }

    @ApiOperation(value = "查看列表数据")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        Example example = new Example(User.class);
        example.orderBy("id").desc();
        Page<User> page = PageHelper.startPage(pageNum, pageSize, true, false, false);
        userService.selectByExample(example);
        PageInfo<User> info = new PageInfo<>(page.getResult());
        return R.ok(info);
    }

}